package demo;

public class MySingleList {
    static class ListNode {
        public int val;//存储的数据
        public ListNode next;//存储下一个节点的地址

        public ListNode(int val) {
            this.val = val;
        }
    }

    public ListNode head;//代表当前链表的头节点的引用

    public void creatLink() {
        ListNode listNode1 = new ListNode(12);
        ListNode listNode2 = new ListNode(23);
        ListNode listNode3 = new ListNode(10);
        ListNode listNode4 = new ListNode(65);
        listNode1.next = listNode2;
        listNode2.next = listNode3;
        listNode3.next = listNode4;
        head = listNode1;
    }

    public void display() {
        ListNode cur = head;
        //如果说 把整个链表 遍历完成 那么就需要 head == null
        //如果说 你遍历到链表的尾巴 head.next == null
        while(cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key){
        ListNode cur = head;
        while(cur != null) {
            if(cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    //得到单链表的长度
    public int size(){
        ListNode cur = head;
        int count = 0;
        while(cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }

    //头插法
    public void addFirst(int data){
        ListNode node = new ListNode(data);
        node.next = head;
        head = node;
    }

    //尾插法
    public void addLast(int data){
        ListNode node = new ListNode(data);
        if(head == null) {
            head = node;
            return;
        }
        ListNode cur = head;
        while(cur.next != null) {
            cur = cur.next;
        }
        cur.next = node;
    }
    //任意位置插入,第一个数据节点为0号下标
    public void addIndex(int index,int data) throws InterruptedException{
        checkIndex(index);
        if(index == 0) {
            addFirst(data);
            return;
        }
        if(index == size()) {
            addLast(data);
            return;
        }
        ListNode node = new ListNode(data);
        ListNode cur = findIndexSubOne(index);

//        ListNode cur = head;
//        while(index != 0) {
//            cur = cur.next;
//            index--;
//        }

        node.next = cur.next;
        cur.next = node;
    }
    //找到index-1位置的节点的地址
    private ListNode findIndexSubOne(int index) {
        ListNode cur = head;
        int count = 0;
        while(count != index-1) {
            cur = cur.next;
            count++;
        }
        return cur;
    }
    private void checkIndex(int index) throws ListIndexOutOfException {
        if (index < 0 || index > size()) {
            throw new ListIndexOutOfException("index位置不合法！");
        }
    }

    //删除第一次出现关键字为key的节点
    public void remove(int key){
        if(head.val == key) {
            head = head.next;
            return;
        }
        ListNode cur = searchPrev(key);
        if(cur == null) {
            return;
        }
        cur.next = cur.next.next;


//        ListNode cur = head;
//        while(cur != null) {
//            if((cur.next).val == key) {
//                cur.next = cur.next.next;
//            }
//            cur = cur.next;
//        }
    }
    //找到关键字key的前一个节点
    private ListNode searchPrev(int key) {
        if(head == null) {
            return null;//一个节点都没有
        }
        ListNode cur = head;
        while(cur.next != null) {
            if(cur.next.val == key) {
                return cur;
            }
            cur = cur.next;
        }
        return null;//没有你要删除的节点
    }


    //删除所有值为key的节点
    public void removeAllKey(int key){
        if(head == null) {
            return;
        }
//        while(head.val == key) {
//            head = head.next;
//        }
        ListNode prev = head;
        ListNode cur = head.next;
        while (cur != null) {
            if(cur.val == key) {
                prev.next = cur.next;
                cur = cur.next;
            }else {
                prev = cur;
                cur = cur.next;
            }
        }
        if(head.val == key) {
            head = head.next;
        }

    }

    //保证链表当中 所有的节点 都可以被回收
    public void clear() {
        head = null;

    }
}
